import { ApiResponse, GetUserParams, GetUsersParams, GetUsersResponse, UserDTO } from '@/app/types/api';

class ApiClient {
  private async request<T>(endpoint: string, options: RequestInit = {}): Promise<ApiResponse<T>> {
    const response = await fetch(endpoint, {
      ...options,
      headers: {
        'Content-Type': 'application/json',
        ...options.headers,
      },
    });

    return response.json();
  }

  async getUser({ id }: GetUserParams) {
    return this.request<UserDTO>(`/api/users/${id}`);
  }

  async getUsers(params?: GetUsersParams) {
    const searchParams = new URLSearchParams();
    if (params?.cursor) searchParams.set('cursor', params.cursor);
    if (params?.limit) searchParams.set('limit', params.limit.toString());

    return this.request<GetUsersResponse>(`/api/users?${searchParams.toString()}`);
  }
}

export const apiClient = new ApiClient();